﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;

namespace Zocono.WCS.Domain.ScWorkModelDomain
{
    public class ScWorkModelService : IScWorkModelService
    {
        public IScWorkModelInfoRepository _IScWorkModelInfoRepository;
        public AutoMapper.IMapper _Mapper;
        private readonly ISnowflake _snowflake;

        public ScWorkModelService(IScWorkModelInfoRepository iScWorkModelInfoRepository,AutoMapper.IMapper mapper, ISnowflake snowflake)
        {
            _IScWorkModelInfoRepository = iScWorkModelInfoRepository;
            _Mapper = mapper;
            this._snowflake = snowflake;
        }

        public async Task<bool> CreateScWorkModelInfoAsync(ScWorkModelInfoDto scWorkModelInfoDto)
        {
            var scWorkModelInfo = _Mapper.Map<ScWorkModelInfoDto, ScWorkModelInfo>(scWorkModelInfoDto);
            scWorkModelInfo.ID = _snowflake.GetId();
            var isOk = await _IScWorkModelInfoRepository.InsertAsync(scWorkModelInfo);
            return isOk;
        }

        public async Task<ScWorkModelInfoDto> GetScWorkModelByIdAsync(long id)
        {
            var scWorkModelInfo = await _IScWorkModelInfoRepository.GetByIdAsync(id);
            var scWorkModelInfoDto = _Mapper.Map<ScWorkModelInfo, ScWorkModelInfoDto>(scWorkModelInfo);
            return scWorkModelInfoDto;
        }

        public async Task<List<ScWorkModelInfo>> GetScWorkModelInfo()
        {
            return await _IScWorkModelInfoRepository.GetListAsync();
        }

        public async Task<List<ScWorkModelInfoDto>> GetScWorkModelInfoDto()
        {
            var scWorkModelInfo = await _IScWorkModelInfoRepository.GetListAsync();
            var scWorkModelInfoDto = _Mapper.Map<List<ScWorkModelInfo>, List<ScWorkModelInfoDto>>(scWorkModelInfo);
            return scWorkModelInfoDto;
        }

        public async Task<bool> UpdateScWorkModelInfo(ScWorkModelInfoDto scWorkModelInfoDto)
        {
            ScWorkModelInfo scWorkModelInfo = new ScWorkModelInfo();
            _Mapper.Map<ScWorkModelInfoDto, ScWorkModelInfo>(scWorkModelInfoDto, scWorkModelInfo);
           return await _IScWorkModelInfoRepository.UpdateAsync(l=>new ScWorkModelInfo { WorkModel=scWorkModelInfo.WorkModel,IsLock=scWorkModelInfo.IsLock}, l=>l.ID==scWorkModelInfo.ID);
        }

        public async Task<bool> DeleteScWorkModelInfoByIdAsync(long id)
        {
            return await _IScWorkModelInfoRepository.DeleteByIdAsync(id);
        }
    }
}
